﻿using System;
using System.Web;

namespace SmsMeta
{
    public class SMS : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            RessourceQuery REST = new RessourceQuery(context.Request);

            if (!REST.IsValid)
            {
                context.Response.StatusCode = REST.StatusCode;
                context.Response.End();
            }

            // Requires desired object
            if (REST.IsImplemented == true)
            {
                try
                {
                    DirectRequest drReq = new DirectRequest(REST);

                    context.Response.Write(drReq.GetObjectOfQuery().OuterXml);
                    context.Response.StatusCode = REST.StatusCode;
                    context.Response.ContentType = REST.ContentType;
                    context.Response.Flush();
                }
                catch (Exception ex)
                {
                    // Here transmit business error
                        StatusMessage status = new StatusMessage(StatusType.Failure, ex.Message);
                        context.Response.Write(status.Doc.OuterXml);
                        context.Response.StatusCode = REST.StatusCode;
                        context.Response.ContentType = REST.ContentType;
                        context.Response.Flush();
                        context.Response.End();
                }
            }
            else
            {
                context.Response.StatusCode = REST.StatusCode;
                context.Response.End();
            }
        }


        public Boolean IsReusable
        {
            get { return false; }
        }
    }

}
